<template>
  <div ref="dialogWrap" class="dw-dialog-wrap" @click="handleClickMask">
    <div class="dw-dialog-container">
      <div v-if="title || $slots.title" class="dw-dialog-header">
        <template v-if="title">{{ title }}</template>
        <slot v-else name="title"></slot>
        <i class="el-icon-close close-icon" @click="handleCancel"></i>
      </div>
      <div class="dw-dialog-content" :style="width ? { width } : {}">
        <slot></slot>
      </div>
      <div v-if="!hideFooter" class="dw-dialog-footer">
        <slot name="footer">
          <el-button
            type="primary"
            :disabled="confirmDisabled"
            @click="handleConfirm"
            >{{ confirmText }}</el-button
          >
          <el-button :disabled="cancelDisabled" @click="handleCancel">{{
            cancelText
          }}</el-button></slot
        >
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "DialogWrap",
  props: {
    closeOnClickModal: {
      type: Boolean,
      default: false,
    },
    onConfirm: {
      type: Function,
      default: () => Promise.resolve(true),
    },
    onCancel: {
      type: Function,
      default: () => Promise.resolve(),
    },
    title: {
      type: String,
      default: "",
    },
    hideFooter: {
      type: Boolean,
      default: false,
    },
    confirmText: {
      type: String,
      default: "确定",
    },
    cancelText: {
      type: String,
      default: "取消",
    },
    confirmDisabled: {
      type: Boolean,
      default: false,
    },
    cancelDisabled: {
      type: Boolean,
      default: false,
    },
    width: {
      type: String,
      default: "",
    },
  },
  methods: {
    handleClickMask(e) {
      if (e.target === this.$refs.dialogWrap && this.closeOnClickModal) {
        this.$parent.$closeDialog();
      }
    },

    handleConfirm() {
      const res = this.onConfirm();
      if (res instanceof Promise) {
        res.then((data = true) => {
          this.$parent.$closeDialog(data);
        });
      } else if (res) {
        this.$parent.$closeDialog(res);
      }
    },

    handleCancel() {
      const res = this.onCancel();
      if (res instanceof Promise) {
        res.then(() => {
          this.$parent.$closeDialog();
        });
      } else if (res) {
        this.$parent.$closeDialog();
      }
    },
  },
};
</script>
<style scoped>
.dw-dialog-wrap {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  opacity: 0;
}
.dw-dialog-wrap:last-child {
  opacity: 1;
  background-color: rgba(0, 0, 0, 0.3);
}
.dw-dialog-container {
  display: inline-block;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  min-width: 300px;
  margin: auto;
  background-color: #fff;
  border-radius: 4px;
  overflow: hidden;
}
.dw-dialog-header {
  position: relative;
  padding-left: 24px;
  border-bottom: 1px solid #e4e7eb;
  color: rgba(0, 0, 0, 0.85);
  font-size: 16px;
  font-weight: 700;
  text-align: left;
  line-height: 48px;

  background: #fff;
  /* border-bottom: 1px solid #eeeeee; */
}

.dw-dialog-content {
  max-height: 80vh;
  min-height: 200px;
  padding: 0 24px;
  overflow: auto;
  box-sizing: border-box;
}

.dw-dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  /* margin: 0 24px; */
  padding: 12px 0;

  background: #ffffff;
  border-top: 1px solid #eeeeee;
  box-sizing: border-box;
}

.close-icon {
  position: absolute;
  top: 50%;
  right: 20px;

  font-size: 16px;
  font-weight: bold;

  transform: translateY(-50%);
  cursor: pointer;
  color: #8c8c8c;
}
</style>
